package com.ycp.erp.warehouse.ui.binder;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.one.common.view.multitytype.adapter.BaseItemBinder;
import com.one.common.view.multitytype.adapter.BaseViewHolderMulti;
import com.ycp.erp.R;
import com.ycp.erp.warehouse.model.bean.Warehouse;

import androidx.annotation.NonNull;

/**
 * Class Des:
 * Created by bjh on 2021/8/24.
 */
public class MoveWarehouseBinder extends BaseItemBinder<Warehouse> {
    public MoveWarehouseBinder() {
        super(R.layout.item_move_warehouse);
    }

    @Override
    protected void bindView(@NonNull BaseViewHolderMulti holder, Warehouse item) {
        ImageView ivSelect = holder.getView(R.id.ivSelect);
        View ivAdd = holder.getView(R.id.ivAdd);
        TextView tvStockNum = holder.getView(R.id.tvStockNum);
        TextView tvPrice = holder.getView(R.id.tvPrice);
        View ivReduce = holder.getView(R.id.ivReduce);
        EditText etNum = holder.getView(R.id.etGoodsNum);
        View vLine = holder.getView(R.id.vLine);
        SpannableString spannableString = new SpannableString("当前库存："+item.getGoodsTotalNum());
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#E79659"));
        spannableString.setSpan(colorSpan, 5, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        tvStockNum.setText(spannableString);
        etNum.setText(String.format("%s", item.getOutGoodsNum()));
        tvPrice.setText(String.format("销售价格：￥%s", item.getGoodsPrice()));
        if (item.isSelect()) {
            ivSelect.setImageResource(R.mipmap.ic_selected_circle);
        } else {
            ivSelect.setImageResource(R.mipmap.ic_unselected_circle);
        }
        ivSelect.setOnClickListener(v -> {
            item.setSelect(!item.isSelect());
            if (item.isSelect()) {
                ivSelect.setImageResource(R.mipmap.ic_selected_circle);
            } else {
                ivSelect.setImageResource(R.mipmap.ic_unselected_circle);
            }
        });
        ivAdd.setOnClickListener(v -> {
            if (item.getGoodsTotalNum()>item.getOutGoodsNum()){
                item.setOutGoodsNum(item.getOutGoodsNum() + 1);
                etNum.setText(String.format("%s", item.getOutGoodsNum()));
            }
        });
        ivReduce.setOnClickListener(v -> {
            if (item.getOutGoodsNum() > 0) {
                item.setOutGoodsNum(item.getOutGoodsNum() - 1);
                etNum.setText(String.format("%s", item.getOutGoodsNum()));
            }
        });
        int position = getPosition(holder);
        if (position == 0) {
            vLine.setVisibility(View.GONE);
        } else {
            vLine.setVisibility(View.VISIBLE);
        }
    }
}
